package com.cbh.domain.service;

import java.util.List;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cbh.common.id.IdWorker;
import com.cbh.entity.Area;
import com.cbh.entity.PlatePrefix;
import com.cbh.mapper.PlatePrefixMapper;

@Service
public class PlatePrefixService {
	private static final Log logger = LogFactory.getLog(PlatePrefixService.class);
	@Autowired
	PlatePrefixMapper platePrefixMapper;
	@Autowired
	AreaService areaService;

	/**
	 * 保存车牌前缀信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String save(PlatePrefix ob) throws Exception {

		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		if (null == ob.getPpValue()) {
			throw new Exception("请传入车牌前缀");
		}
		ob.setPpValue(ob.getPpValue().toUpperCase(Locale.SIMPLIFIED_CHINESE));
		if (null != ob.getPp_areaId()) {
			Area area = areaService.getOne(ob.getPp_areaId());
			if (null == area) {
				throw new Exception("区域不存在");
			}
			ob.setPp_areaName(area.getAreaName());
		}
		PlatePrefix ppv = new PlatePrefix().ppv(ob.getPpValue());
		List<PlatePrefix> obs = platePrefixMapper.queryAll(ppv);
		ppv = null == obs || obs.isEmpty() ? null : obs.get(0);

		if (StringUtils.isEmpty(ob.getPpId())) {
			if (null == ob.getPp_areaId()) {
				throw new Exception("请传入归属地区");
			}
			if (null != ppv) {
				throw new Exception("车牌前缀已归属在" + ppv.getPp_areaName());
			}
			return insert(ob);
		}
		if (null != ppv && !ob.getPpId().equals(ppv.getPpId())) {
			throw new Exception("车牌前缀已归属在" + ppv.getPp_areaName());
		}
		return update(ob);
	}

	/**
	 * 保存车牌前缀信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	private String insert(PlatePrefix ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		ob.setPpId(IdWorker.getId().toString());
		try {
			Integer re = platePrefixMapper.insert(ob);
			if (re > 0) {
				return ob.getPpId();
			} else {
				throw new Exception("保存数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("保存数据失败");
		}
	}

	/**
	 * 保存车牌前缀信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	private String update(PlatePrefix ob) throws Exception {
		try {
			Integer re = platePrefixMapper.update(ob);
			if (re > 0) {
				return ob.getPpId();
			} else {
				throw new Exception("更新数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("更新数据失败");
		}
	}

	/**
	 * 真删除车牌前缀
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public boolean delete(String id) throws Exception {
		PlatePrefix ob = new PlatePrefix().id(id);
		try {
			Integer re = platePrefixMapper.delect(ob);
			return re == 1;
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("删除数据失败");
		}
	}

	/**
	 * 根据proId获取车牌前缀信息
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public PlatePrefix getOne(String pmId) throws Exception {
		if (null == pmId) {
			throw new Exception("主键为空");
		}
		PlatePrefix ob = new PlatePrefix().id(pmId);
		try {
			ob = platePrefixMapper.queryOne(ob);
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return ob;
	}

	/**
	 * 获取全部有效的车牌前缀
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<PlatePrefix> getAll() throws Exception {
		List<PlatePrefix> obs = null;
		try {
			obs = platePrefixMapper.queryAll(new PlatePrefix());
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return obs;
	}
}
